<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="卦象" prop="lGuaXiang">
        <el-input
          v-model="queryParams.lGuaXiang"
          placeholder="请输入卦象"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="卦名" prop="lGuaName">
        <el-input
          v-model="queryParams.lGuaName"
          placeholder="请输入卦名"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="卦辞" prop="lGuaCi">
        <el-input
          v-model="queryParams.lGuaCi"
          placeholder="请输入卦辞"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="卦词" prop="lGuaWord">
        <el-input
          v-model="queryParams.lGuaWord"
          placeholder="请输入卦词"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="象曰" prop="lXiangYue">
        <el-input
          v-model="queryParams.lXiangYue"
          placeholder="请输入象曰"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="卦象描述" prop="lXiangShu">
        <el-input
          v-model="queryParams.lXiangShu"
          placeholder="请输入卦象描述"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="彖" prop="lTuan">
        <el-input
          v-model="queryParams.lTuan"
          placeholder="请输入彖"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="象" prop="lXiang">
        <el-input
          v-model="queryParams.lXiang"
          placeholder="请输入象"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="文言" prop="lWenYan">
        <el-input
          v-model="queryParams.lWenYan"
          placeholder="请输入文言"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="系辞" prop="lXiCi">
        <el-input
          v-model="queryParams.lXiCi"
          placeholder="请输入系辞"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="说卦" prop="lShuoGua">
        <el-input
          v-model="queryParams.lShuoGua"
          placeholder="请输入说卦"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="序卦" prop="lXuGua">
        <el-input
          v-model="queryParams.lXuGua"
          placeholder="请输入序卦"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="杂卦" prop="lZaGua">
        <el-input
          v-model="queryParams.lZaGua"
          placeholder="请输入杂卦"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['life:yi:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['life:yi:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['life:yi:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['life:yi:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="yiList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="卦序" align="center" prop="lGuaId" />
      <el-table-column label="卦象" align="center" prop="lGuaXiang" />
      <el-table-column label="卦名" align="center" prop="lGuaName" />
      <el-table-column label="卦辞" align="center" prop="lGuaCi" />
      <el-table-column label="卦词" align="center" prop="lGuaWord" />
      <el-table-column label="象曰" align="center" prop="lXiangYue" />
      <el-table-column label="卦象描述" align="center" prop="lXiangShu" />
      <!-- <el-table-column label="彖" align="center" prop="lTuan" />
      <el-table-column label="象" align="center" prop="lXiang" />
      <el-table-column label="文言" align="center" prop="lWenYan" />
      <el-table-column label="系辞" align="center" prop="lXiCi" />
      <el-table-column label="说卦" align="center" prop="lShuoGua" />
      <el-table-column label="序卦" align="center" prop="lXuGua" />
      <el-table-column label="杂卦" align="center" prop="lZaGua" /> -->
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作"  width="180" align="center"  class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['life:yi:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['life:yi:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改周易对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="yiRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="卦象" prop="lGuaXiang">
          <el-input v-model="form.lGuaXiang" placeholder="请输入卦象" />
        </el-form-item>
        <el-form-item label="卦名" prop="lGuaName">
          <el-input v-model="form.lGuaName" placeholder="请输入卦名" />
        </el-form-item>
        <el-form-item label="卦辞" prop="lGuaCi">
          <el-input v-model="form.lGuaCi" placeholder="请输入卦辞" />
        </el-form-item>
        <el-form-item label="卦词" prop="lGuaWord">
          <el-input v-model="form.lGuaWord" placeholder="请输入卦词" />
        </el-form-item>
        <el-form-item label="象曰" prop="lXiangYue">
          <el-input v-model="form.lXiangYue" placeholder="请输入象曰" />
        </el-form-item>
        <el-form-item label="卦象描述" prop="lXiangShu">
          <el-input v-model="form.lXiangShu" placeholder="请输入卦象描述" />
        </el-form-item>
        <!-- <el-form-item label="彖" prop="lTuan">
          <el-input v-model="form.lTuan" placeholder="请输入彖" />
        </el-form-item>
        <el-form-item label="象" prop="lXiang">
          <el-input v-model="form.lXiang" placeholder="请输入象" />
        </el-form-item>
        <el-form-item label="文言" prop="lWenYan">
          <el-input v-model="form.lWenYan" placeholder="请输入文言" />
        </el-form-item>
        <el-form-item label="系辞" prop="lXiCi">
          <el-input v-model="form.lXiCi" placeholder="请输入系辞" />
        </el-form-item>
        <el-form-item label="说卦" prop="lShuoGua">
          <el-input v-model="form.lShuoGua" placeholder="请输入说卦" />
        </el-form-item>
        <el-form-item label="序卦" prop="lXuGua">
          <el-input v-model="form.lXuGua" placeholder="请输入序卦" />
        </el-form-item>
        <el-form-item label="杂卦" prop="lZaGua">
          <el-input v-model="form.lZaGua" placeholder="请输入杂卦" />
        </el-form-item> -->
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Yi">
import { listYi, getYi, delYi, addYi, updateYi } from "@/api/life/yi";

const { proxy } = getCurrentInstance();

const yiList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    lGuaXiang: null,
    lGuaName: null,
    lGuaCi: null,
    lGuaWord: null,
    lXiangYue: null,
    lXiangShu: null,
    lTuan: null,
    lXiang: null,
    lWenYan: null,
    lXiCi: null,
    lShuoGua: null,
    lXuGua: null,
    lZaGua: null,
  },
  rules: {
    lGuaXiang: [
      { required: true, message: "卦象不能为空", trigger: "blur" }
    ],
    lGuaName: [
      { required: true, message: "卦名不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询周易列表 */
function getList() {
  loading.value = true;
  listYi(queryParams.value).then(response => {
    yiList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    lGuaId: null,
    lGuaXiang: null,
    lGuaName: null,
    lGuaCi: null,
    lGuaWord: null,
    lXiangYue: null,
    lXiangShu: null,
    lTuan: null,
    lXiang: null,
    lWenYan: null,
    lXiCi: null,
    lShuoGua: null,
    lXuGua: null,
    lZaGua: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    remark: null
  };
  proxy.resetForm("yiRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.lGuaId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加周易";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _lGuaId = row.lGuaId || ids.value
  getYi(_lGuaId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改周易";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["yiRef"].validate(valid => {
    if (valid) {
      if (form.value.lGuaId != null) {
        updateYi(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addYi(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _lGuaIds = row.lGuaId || ids.value;
  proxy.$modal.confirm('是否确认删除周易编号为"' + _lGuaIds + '"的数据项？').then(function() {
    return delYi(_lGuaIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('life/yi/export', {
    ...queryParams.value
  }, `yi_${new Date().getTime()}.xlsx`)
}

getList();
</script>
